package com.kk.crowd.mvc.handler;

import com.kk.crowd.service.api.AdminService;
import com.kk.crowd.service.api.AuthService;
import com.kk.crowd.service.api.RoleService;
import com.kk.crowd.entity.Auth;
import com.kk.crowd.entity.Role;
import com.kk.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private AuthService authService;

    @ResponseBody
    @RequestMapping("save/AdminAuth.json")
    public ResultEntity<String> saveAdminAuth(@RequestBody Map<String, List<Integer>> map) {
        roleService.saveAdminRole (map);
        return ResultEntity.successWithoutData ( );
    }

    @ResponseBody
    @RequestMapping("/assign/do/role/assign/auth.json")
    public ResultEntity<String> saveRoleAuthRelathinship(@RequestBody Map<String, List<Integer>> map) {
        authService.saveRoleAuthRelathinship (map);
        return ResultEntity.successWithoutData ( );
    }

    @ResponseBody
    @RequestMapping("assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(@RequestParam("roleId") Integer roleId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId (roleId);
        return ResultEntity.successWithoutData (authIdList);
    }

    @ResponseBody
    @RequestMapping("/assgin/get/all/auth.json")
    public ResultEntity<List<Auth>> getAuthList() {
        List<Auth> authList = authService.getAll ( );
        return ResultEntity.successWithoutData (authList);
    }

    @RequestMapping("/assign/do/role/assign.html")
    public String saveAssign(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            // 我们允许用户在页面上取消所有已分配角色再提交表单，所以可以不提供 roleIdList 请求参数
            // 设置 required=false 表示这个请求参数不是必须的
            @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList
    ) {

        adminService.saveAdminRoleRelationship (adminId, roleIdList);
        return "redirect:/admin/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }


    /**
     * 查看用户对应的权限
     *
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("assign/to/role/page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap
    ) {
        // 1.查询已分配角色
        List<Role> assignedRoleList = roleService.getAssignedRole (adminId);
        // 2.查询未分配角色
        List<Role> unAssignRoleList = roleService.getUnAssignedRole (adminId);
        // 3.存入模型（本质上其实是：request.setAttribute("attrName",attrValue);
        modelMap.addAttribute ("assignedRoleList", assignedRoleList);
        modelMap.addAttribute ("unAssignRoleList", unAssignRoleList);
        // 4.回传adminId,用户保存
        modelMap.addAttribute ("adminId", adminId);

        return "assign-role";
    }
}
